package com.cms.ctrl;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cms.domain.Item;
import com.cms.service.ItemService;
import com.core.page.Pagination;
import com.core.spring.JsonView;
import com.wxcms.domain.AccountFans;

/**
 * 
 */

@Controller
@RequestMapping("/item")
public class ItemCtrl{

	@Autowired
	private ItemService entityService;

	@RequestMapping(value = "/itemManager")
	public  ModelAndView itemManager(String id_sbj,Pagination<AccountFans> pagination){
		ModelAndView modelAndView = new ModelAndView("subject/item/itemManager");
		List pageList = entityService.getList(Long.valueOf(id_sbj), pagination.getPageNum(), pagination.getPageSize());
		if(pageList!=null && pageList.size()>0){
			pagination.setItems(pageList);
			pagination.setTotalItemsCount(entityService.getRowCount(Long.valueOf(id_sbj)));
		}else{
			if(!pagination.isFirstPage()){
				pagination.setPageNum(pagination.getPrevPageNum());
				this.itemManager(id_sbj, pagination);
			}
		}
		
		modelAndView.addObject("pagination", pagination);
		modelAndView.addObject("cur_nav", "subject");
		return modelAndView;
	}
	
	@RequestMapping(value = "/toMerge")
	@ResponseBody
	public String toMerge(HttpServletRequest request){
		String id = request.getParameter("id");
		String idSbj = request.getParameter("idSbj");
		String name = request.getParameter("name");
		String content = request.getParameter("content");
		String type = request.getParameter("type");
		String creater = request.getParameter("creater");

		Item entity = new Item();
		if(id==null || id.equals("")){
			entity.setIdSbj(Long.valueOf(idSbj));
			entity.setName(name);
			entity.setContent(content);
			entity.setType(type);
			entity.setCreater(creater);
			entity.setCreatetime(new Date());
			entityService.add(entity);
		}else{
			List<Item> itemList = entityService.getByName(Long.valueOf(idSbj), name);
			if(itemList.size()==0){
				entity = entityService.getById(Long.valueOf(id));
			}else if(itemList.size()==1){
				Item item_new = itemList.get(0);
				if(item_new.getId()!=Long.valueOf(id)){
					JsonView jv = new JsonView();
					jv.setErrcode(1);
					jv.setMessage("数据项名称重复,请重新填写!");
					return jv.toString();
				}else{
					entity = item_new;
				}
			}else{
				JsonView jv = new JsonView();
				jv.setErrcode(1);
				jv.setMessage("数据项名称重复,请重新填写!");
				return jv.toString();
			}
			
			entity.setName(name);
			entity.setContent(content);
			entity.setType(type);
			entity.setCreater(creater);
			entityService.update(entity);
		}
		
		JsonView jv = new JsonView();
		jv.setData(entity);
		return jv.toString();
	}
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public String delete(HttpServletRequest request){
		String id = request.getParameter("id");
		if(id!=null && !id.equals("")){
			Item entity = entityService.getById(Long.valueOf(id));
			entityService.delete(entity);
		}
		JsonView jv = new JsonView();
		return jv.toString();
	}

}

